package optim;

import java.io.*;
import database.EventSerializer;
import event.Event;

public class BatchSizeCalculator {
	private static BatchSizeCalculator sInstance = null;

	public static BatchSizeCalculator instance() {
		if (sInstance == null)
			sInstance = new BatchSizeCalculator();

		return sInstance;
	}

	public BatchSizeCalculator() {

	}

	public void doTest(int minSize, int maxSize, int stepSize, int streamID,
			byte[] eventData, String fileName) {
		try {
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter out = new BufferedWriter(fstream);
			out
					.write("batch size,inserts per second,seconds for storing 75.000 events");
			out.newLine();

			EventSerializer es = EventSerializer.instance();
			Timer timer = new Timer();

			int batchSize = minSize;
			while (batchSize <= maxSize) {
				es.setInsertBatchSize(batchSize);
				es.truncateSerializedEvents();
				timer.start();

				for (int i = 0; i < 75000; ++i) {
					Event dummy = new Event(streamID, true, true, true,
							eventData);
					es.storeEvent(dummy);
				}
				es.commitInsertBatch();
				timer.stop();

				long nbPerSecond = (long) (75000 / timer.getTimeSecs());
				out.write(batchSize + "," + nbPerSecond + ","
						+ timer.getTimeSecs());
				out.newLine();
				System.out.println(batchSize + "," + nbPerSecond + ","
						+ timer.getTimeSecs());

				batchSize += stepSize;
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
